<template>
    <div v-loading="loading" style="padding-bottom:10px" :class="className" :id="id" :style="{height:height,width:width}"></div>       
</template>
<script>
// 引入 ECharts 主模块
const echarts = require('echarts/lib/echarts');

require('echarts/lib/chart/line');
// 引入提示框和标题组件
require('echarts/lib/component/tooltip');
require('echarts/lib/component/title');
require('echarts/lib/component/legend');
import { listOrderStatisticChart } from 'api/serviceManage';
export default {
    name: 'barPercent',
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        id: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '200px'
        },
        height: {
            type: String,
            default: '200px'
        }
    },
    created() {
        this.getChar();
    },
    data() {
        return {
      // 格式化需求数据
            dateList: {
        // 横坐标集合
                timeList: [],
        // 纵坐标集合
                moneyList: []
            },
            loading: false
        };
    },
    mounted() {
    // this.initChart();
    },
    methods: {
        getChar() {
            this.loading = true;
            listOrderStatisticChart().then(data => {
                this.loading = false;
                if (data.data.content) {
                    data.data.content.forEach(item => {
                        this.dateList.timeList.unshift(item.datetime);
                        this.dateList.moneyList.unshift(item.totalmoney);
                        this.initChart();
                    });
                }
            }).catch(Error => {
                this.$message.error(Error)
                this.loading = false;
            })
        },
        initChart() {
            this.chart = echarts.init(document.getElementById(this.id));
            this.chart.setOption({
                backgroundColor: '#fff',
                title: {
                    text: '销售额增长趋势图',
                    textStyle: {
                        fontWeight: 'normal',
                        fontSize: 16,
                        color: '#333',
                        padding: '0px 0px 10px 0px'
                    },
                    left: '50%'
                },
                tooltip: {
                    trigger: 'axis',
                    show: true,
                    axisPointer: {
                        lineStyle: {
                            color: 'rgba(255, 255, 255, 0.1)'
                        }
                    }
                },
                legend: {
                    icon: 'rect',
                    itemWidth: 14,
                    itemHeight: 5,
                    itemGap: 13,
                    data: ['销售额'],
                    right: '4%',
                    selectedMode: false,
                    textStyle: {
                        fontSize: 12,
                        color: '#333'
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '12%',
                    containLabel: true
                },
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        axisLabel: {
                            rotate: 45,
                            interval: 0
                        },
                        axisLine: {
                            lineStyle: {
                                color: '#555'
                            }
                        },
                        data: this.dateList.timeList
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        name: '销售额/元',
                        left: '5px',
                        axisTick: {
                            show: false
                        },
                        axisLine: {
                            lineStyle: {
                                color: '#444'
                            }
                        },
                        axisLabel: {
                            margin: 10,
                            textStyle: {
                                fontSize: 14
                            }
                        },
                        splitLine: {
                            lineStyle: {
                                color: '#eee'
                            }
                        }
                    }
                ],
                series: [
                    {
                        name: '销售额',
                        type: 'line',
                        smooth: true,
                        symbol: 'circle',
                        symbolSize: 5,
                        showSymbol: false,
                        lineStyle: {
                            normal: {
                                width: 1
                            }
                        },
                        areaStyle: {
                            normal: {
                                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                                    [
                                        {
                                            offset: 0,
                                            color: 'rgba(174,175,180, 0.9)'
                                        },
                                        {
                                            offset: 0.8,
                                            color: 'rgba(174,175,180, 0.9)'
                                        }
                                    ],
                  false
                ),
                                shadowColor: 'rgba(0, 0, 0, 0.1)',
                                shadowBlur: 10
                            }
                        },
                        itemStyle: {
                            normal: {
                                color: 'rgb(255,94,44)',
                                borderColor: 'rgba(137,189,2,0.27)',
                                borderWidth: 12,
                                lineStyle: {
                                    color: 'rgba(174,175,180, 0.9)'
                                }
                            }
                        },
                        data: this.dateList.moneyList
                    }
                ]
            });
        }
    }
};
</script>